package org.hhchat.leetcode.stack;

import java.util.Stack;

/**
 * Created by hh23485 on 2017/8/27.
 * https://leetcode.com/problems/valid-parentheses/description/
 */
public class code20_easy {
    static class Solution {
        public boolean isValid(String s) {
            Stack<Character> characters = new Stack<>();
            char[] arrays = s.toCharArray();
            for (char c : arrays) {
                if (c == '(' || c == '{' || c == '[') {
                    characters.push(c);
                }
                else if(c==')' && characters.size()>0 && characters.peek()=='('){
                    characters.pop();
                }else if(c=='}' && characters.size()>0 &&  characters.peek()=='{'){
                    characters.pop();
                } else if (c == ']' && characters.size()>0 &&  characters.peek() == '[') {
                    characters.pop();
                }else{
                    return false;
                }
            }
            return characters.empty();
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.isValid("]"));
    }
}
